package com.lai.note.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lai.note.R;
import com.lai.note.constant.Constant;
import com.lai.note.dto.ResponseResult;
import com.lai.note.service.UserBaseService;
import com.lai.note.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterFragment extends Fragment {
    public RegisterFragment(){
        super(R.layout.fragment_register);
    }

    //注册相关信息：手机号，验证码，第一次输入的密码，第二次输入的确认密码
    private EditText et_phone;
    private EditText et_code;
    private EditText et_loginPwd;
    private EditText et_confirmPwd;
    private String phone;
    private String code;
    private String loginPwd;
    private String confirmPwd;

    //获取验证码按钮
    private Button btn_code;
    //注册提交按钮
    private Button btn_register;

    //retrofit对象
    private Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constant.ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private UserBaseService userBaseService = retrofit.create(UserBaseService.class);

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListeners();
    }

    private void initView(){
        et_phone = getView().findViewById(R.id.et_userPhone);
        et_code = getView().findViewById(R.id.code);
        et_loginPwd = getView().findViewById(R.id.et_loginPwd);
        et_confirmPwd = getView().findViewById(R.id.et_confirmPwd);
        btn_register = getView().findViewById(R.id.btn_register);
        btn_code = getView().findViewById(R.id.btn_code);

    }

    private void setListeners(){

        btn_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = et_phone.getText().toString();
                Call<ResponseResult> call = userBaseService.getCode(phone);
                call.enqueue(new Callback<ResponseResult>() {
                    @Override
                    public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                        ResponseResult res = response.body();
                        System.out.println("res:-->"+res);
                        if(res.getStatus()==200){
                            System.out.println("200");
                            ToastUtil.display(getActivity(),"验证码为："+res.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseResult> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
        });

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = et_phone.getText().toString();
                code = et_code.getText().toString();
                loginPwd = et_loginPwd.getText().toString();
                confirmPwd = et_confirmPwd.getText().toString();
                Call<ResponseResult> call = userBaseService.fetchRegisterResult(phone,code,loginPwd,confirmPwd,175.3,25.2);
                call.enqueue(new Callback<ResponseResult>() {
                    @Override
                    public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                        ResponseResult res = response.body();//返回的结果
                        if(res.getStatus()==200){
                            ToastUtil.display(getActivity(),"注册成功！\n请开始您愉快的记录之旅吧！");
                        }else{
                            ToastUtil.display(getActivity(),res.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseResult> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
        });
    }








}
